<?php

namespace App\Admin\Actions\Audit;

use App\Library\ClLib;
use App\Library\Logic\AuditLogic;
use App\Library\Logic\BankLogic;
use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\UserAccountLogic;
use App\Library\Logic\UserDmLogic;
use App\Models\AuditModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;

class AuditBankAction extends RowAction
{
    public $name = '审核';

    /**
     *
     * @param AuditModel $model
     * @param Request $request
     * @return Response
     * @throws GuzzleException
     *
     */
    public function handle(AuditModel $model, Request $request): Response
    {
        if (!empty($audit->content['msg'])) return $this->response()->error($audit->content['msg'])->refresh();
        $instId = 'instId:' . $model->content;
        $model->operate_id = Admin::user()->id;
        $model->status = $request->status;
        if ((int)$request->status === 1) {
            $realName = UserDmLogic::valueByWhere('userId', $model->user_id, 'realName');
            if (is_null($realName)) return $this->response()->error('账号还未实名认证')->refresh();
            $arr = ClLib::bank($realName, $model->content);
            if (empty($arr['code'])) {
                AuditLogic::bankError($model, $arr, $arr['message']);
                return $this->response()->error('审核失败：' . $arr['data']['remark'])->refresh();
            } else if ($arr['chargeStatus'] === 0) {
                AuditLogic::bankError($model, $arr, $arr['message']);
                return $this->response()->error('审核失败：' . $arr['data']['remark'])->refresh();
            } else if ($arr['data']['result'] === '02') {
                AuditLogic::bankError($model, $arr, $arr['data']['remark']);
                return $this->response()->error('审核失败：' . $arr['data']['remark'])->refresh();
            } else if ($arr['data']['cardCategory'] !== '借记卡') {
                $msg = '该银行卡不是借记卡类型';
                AuditLogic::bankError($model, $arr, $msg);
                return $this->response()->error('审核失败：' . $msg)->refresh();
            }
            BankLogic::insertForModel([
                'userId' => $model->user_id,
                'instId' => cache($instId),
                'bankName' => $arr['data']['bankName'],
                'cardNo' => $model->content,
                'info' => $arr,
            ]);
            $userAccount = UserAccountLogic::findByWhere(['userId' => $model->user_id, 'paymentMethodId' => 3]);
            if (is_null($userAccount)) {
                UserAccountLogic::insertForModel([
                    'userId' => $model->user_id,
                    'paymentMethodId' => 3,
                    'name' => $realName,
                    'identity' => $model->content,
                    'nickname' => $arr['data']['bankName'],
                ]);
            } else {
                $userAccount->identity = $model->content;
                $userAccount->nickname = $arr['data']['bankName'];
                $userAccount->save();
            }
            $model->ok_reason = is_null($request->reason) ? '' : $request->reason;
        } else $model->fail_reason = is_null($request->reason) ? '' : $request->reason;
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.bank.audit');
        cache()->forget($instId);
        return $this->response()->success('操作成功！')->refresh();
    }

    public function form(): void
    {
        $this->select('status', '审核')->options([1 => '同意', 2 => '拒绝'])->default(2)->required();
        $this->textarea('reason', '原因')->rules('nullable|string|max:255');
    }

    public function html(): string
    {
        return "<a class='report-posts btn btn-sm btn-danger'><i class='fa fa-info-circle'></i>确认</a>";
    }
}
